package dao

import (
	"a-system/domain/entity"
	"sync"
)

var (
	sysLoginDaoTemp *SysLoginDao
	sysLoginDaoOnce sync.Once
)

type SysLoginDao struct{}

func SysNewLoginDao() *SysLoginDao {
	sysLoginDaoOnce.Do(func() {
		sysLoginDaoTemp = &SysLoginDao{}
	})
	return sysLoginDaoTemp
}

func (m *SysLoginDao) GetUserByNameAndPwd(userName string, pwd string) entity.SysUserVo {
	var vUser entity.SysUserVo
	_, err := pgCli.Table(&entity.UserDto{}).Where("user_name=? and password=?", userName, pwd).Get(&vUser)
	if err != nil {
		return entity.SysUserVo{}
	}
	return vUser
}

func (m *SysLoginDao) GetUserByAccount(userName string) (entity.SysUser, error) {
	var userPo entity.SysUser
	_, err := pgCli.Table(&entity.SysUser{}).Where("user_name=?", userName).Get(&userPo)
	return userPo, err
}

func (m *SysLoginDao) AddUser(entity *entity.UserDto) (int64, error) {
	return pgCli.InsertOne(&entity)
}

func (m *SysLoginDao) Update(entity *entity.UserDto) (int64, error) {
	return pgCli.Table(&entity).Update(entity)
}
